package org.example.ttms.controller;

import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.text.Text;
import javafx.util.Callback;
import org.example.ttms.customize.BuyTicket;
import org.example.ttms.customize.ChangeTicketWindow;
import org.example.ttms.customize.Pop_ups;
import org.example.ttms.entityClass.User;
import org.example.ttms.entityClass.ticket.ChangeTicket;
import org.example.ttms.entityClass.ticket.Ticket;
import org.example.ttms.entityClass.train.Trips;

import java.util.HashMap;
import java.util.List;

public class ChangeTicketHostController {

    @FXML
    private Text text;

    @FXML
    private TableView<Trips> tableView;

    @FXML
    private TableColumn<Trips, String> tripsIdTableColumn;

    @FXML
    private TableColumn<Trips, String> departureTableColumn;

    @FXML
    private TableColumn<Trips, String> destinationTableColumn;

    @FXML
    private TableColumn<Trips, String> goTimeTableColumn;

    @FXML
    private TableColumn<Trips, String> arrivalTimeTableColumn;

    @FXML
    private TableColumn<Trips, Integer> businessTableColumn;

    @FXML
    private TableColumn<Trips, Integer> firstTableColumn;

    @FXML
    private TableColumn<Trips, Integer> secondTableColumn;

    @FXML
    private TableColumn<Trips, Void> changeTableColumn;

    @FXML
    private Label label;

    private User user;
    public static ObservableList<Trips> observableList = FXCollections.observableArrayList();

    public void initialize() {
        text.setText("改签费:"+ ChangeTicket.price+"元");
        user = UserController.user;
        Ticket ticket = ChangeTicketWindow.ticket;
        //observableList
        Trips trips = new Trips();
        HashMap<String, Trips> hashMap1;
        //显示有座的票
        hashMap1 = trips.obtainBuyTicketTrips3(ticket.getDeparture(),
                ticket.getDestination());
        hashMap1.remove(ticket.getId());
        observableList.clear();
        observableList.addAll(hashMap1.values());

        //tableView
        tripsIdTableColumn.setCellValueFactory(cellData -> cellData.getValue().tripsIdStringProperty());
        departureTableColumn.setCellValueFactory(cellData -> cellData.getValue().departureProperty(
                ticket.getDeparture()
        ));
        destinationTableColumn.setCellValueFactory(cellData -> cellData.getValue().destinationProperty(
                ticket.getDestination()
        ));
        goTimeTableColumn.setCellValueFactory(cellDate -> cellDate.getValue().goTimeProperty2(
                ticket.getDeparture()));
        arrivalTimeTableColumn.setCellValueFactory(cellData -> cellData.getValue().arrivalTimeProperty2(
                ticket.getDestination()));
        businessTableColumn.setCellValueFactory(cellData -> cellData.getValue().businessProperty().asObject());
        firstTableColumn.setCellValueFactory(cellData -> cellData.getValue().firstProperty().asObject());
        secondTableColumn.setCellValueFactory(cellData -> cellData.getValue().secondProperty().asObject());
        changeTableColumn.setCellFactory(new Callback<TableColumn<Trips, Void>, TableCell<Trips, Void>>() {

            @Override
            public TableCell<Trips, Void> call(TableColumn<Trips, Void> tripsVoidTableColumn) {
                return new TableCell<Trips, Void>() {
                    private final Button buyButton = new Button("  改签  ");

                    {
                        buyButton.getStyleClass().add("button7");
                        Pop_ups popUps = new Pop_ups();
                        buyButton.setOnAction(e -> {
                            Trips trips1 = getTableView().getItems().get(getIndex());
                            if (isBuyThisTicket(trips1)) {
                                popUps.pop_ups3("你已经买了该车次车票");
                            } else {
                                //进入买票窗口
                                ticket.setDeparture(ticket.getDeparture());
                                ticket.setDestination(ticket.getDestination());
                                ticket.setGoTime(trips1.goTimeDate(ticket.getDeparture()));
                                ticket.setArrivalTime(trips1.arrivalDate(ticket.getDestination()));
                                ChangeTicketWindow changeTicket = new ChangeTicketWindow();
                                changeTicket.changeTicketWindow2(ticket, trips1);
                            }
                        });
                    }

                    @Override
                    public void updateItem(Void item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty) {
                            setGraphic(null);
                        } else {
                            setGraphic(buyButton);
                        }
                    }
                };
            }
        });

        label.setVisible(observableList.isEmpty());
        observableList.addListener((ListChangeListener.Change<? extends Trips> c) -> {
            label.setVisible(observableList.isEmpty());
        });
        tableView.setItems(observableList);
    }

    //判断该用户是否买了该车次的票
    public boolean isBuyThisTicket(Trips trips) {
        Ticket ticket = new Ticket();
        List<Ticket> list = ticket.obtainTripsAllTicket(trips);
        for(Ticket ticket1 : list){
            if(ticket1.getBuyerName() != null && ticket1.getBuyerName().equals(user.getName()) && ticket1.getStatus().equals("已售出")){
                return true;
            }
        }
        return false;
    }
}
